**************Plotting moments fit***************
//Fuyao Wang
//6.14.2021

clear all
set more off, perm
capture log close
set maxvar 32767
global klmChinaReach2: env klmChinaReach2
global datapath "${klmChinaReach2}/Fuyao_Erlfang"
cd "$datapath\moments_dta"



******all task passing rate of cognitive******

use alltask_rate, clear
merge 1:1 ts_id using "$datapath/Eleanor/Control Groups/controlgroup.dta", nogen keepusing(level* repeat*)


unab skills : level*
local skills : subinstr local skills "level" "", all
foreach skill of local skills {
	sort level`skill' ts_id
	gen index`skill'= sum(!missing(level`skill')) if !missing(level`skill')
}
sort ts_id

keep if levelc2 != .


**Paste over lines 1-70 simulated moments - var46

replace var46 = . if var46 == -99

label var mean_task "Data"
label var ub "95% CI"
label var var46 "Model"

local xlines ""
local xlabels ""
sum levelc2
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum indexc2 if levelc2==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}


twoway line mean_task indexc2, sort lwidth(medthin) || line ub indexc2, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb indexc2, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var46 indexc2, sort msize(small)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks by Level", size(medium)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("Note: The dashed yellow lines indicate the last task at each difficulty level. Within difficulty levels, " "tasks are arranged by the order of the children taking them.", size(small) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("plots/cog/cog_alltask", replace)
	graph export "plots/cog/cog_alltask.pdf", replace	
	
	


******levelc2 only 13******
use levelc2_rate, clear


label var mean_task "Data"
label var ub "95% CI"
**paste over 71-83
label var var7 "Model"

twoway connected mean_task levelc2, sort lwidth(medthin) msize(small) || line ub levelc2, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb levelc2, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var7 levelc2, sort msize(small)   ///
	xlabel(1(1)13, labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks Average Passing Rate at Each Level", size(medium)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("plots/cog/cog_levelc2", replace)
	graph export "plots/cog/cog_levelc2.pdf", replace	
	
	
	
*************c2 conditional on previous level*****************

use artc2_rate_pre1, clear

**Paste over lines 84-260 simulated moments - var9

gen temp = _n

bys l_focus (temp): gen indexc2 = _n

label var mean "Data"
label var ub "95% CI"
label var var9 "Model"
forval i = 2/12{
// 	sum indexc2 if l_focus == `i'
// 	if `r(max)' <= 16 & `r(max)' > 10 {
// 		local xlabels "1(2)`r(max)'"
// 	}
// 	else if `r(max)' <= 10 {
// 		local xlabels "1(1)`r(max)'"
// 	}
// 	else if `r(max)' > 16 {
// 		local xlabels "1 5(5)`r(max)'"
// 	}
	if `i' == 2 | (`i' >= 6 & `i' <= 8) {
		local xlabels `" 3 "1st Task" 8 "2nd Task" 13 "3rd Task" 18 "4th Task" 23 "5th Task" "'
		local xlines "5(5)20"
	}
	else if `i' == 3 | `i' == 9 {
		local xlabels `" 1 "1st Task" 3 "2nd Task" 5 "3rd Task" 7 "4th Task" 9 "5th Task" "'
		local xlines "2(2)8"
	}
	else if `i' == 4 {
		local xlabels `" 1.5 "1st Task" 3.5 "2nd Task" "'
		local xlines "2.5"
	}
	else if `i' == 5 {
		local xlabels `" 3 "1st Task" 8 "2nd Task" "'
		local xlines "5(5)10"
	}
	else if `i' == 10 {
		local xlabels `" 2 "1st Task" 6 "2nd Task" "'
		local xlines "4"
	}
	else if `i' == 11 {
		local xlabels `" 3 "1st Task" 8 "2nd Task" 13 "3rd Task" 18 "4th Task" "'
		local xlines "5(5)15"
	}
	else if `i' == 12 {
		local xlabels `" 2 "1st Task" 5 "2nd Task" 8 "3rd Task" 11 "4th Task" 14 "5th Task" "'
		local xlines "3(3)12"
	}	
	twoway connected mean indexc2 if l_focus == `i', sort lwidth(medthin) msize(small) || line ub indexc2 if l_focus == `i', sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb indexc2 if l_focus == `i', sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var9 indexc2 if l_focus == `i', sort msize(small)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Task Order from the Previous Level that is being Conditional on, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks Average Passing Rate (Level `i')", size(medium)) subtitle("Conditional on Difficulty Level `=`i'-1' Task Performance", size(small)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("The dashed yellow lines indicate the last task at each task from previous level being conditioned on." "Within difficulty levels, tasks are arranged by the order of the children taking them.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin))  ///
	saving("plots/cog/cog_pre1_l`i'", replace)
	graph export "plots/cog/cog_pre1_l`i'.pdf", replace	

}		

/*	
gen indexc2 = _n	


label var mean "Data"
label var ub "95% CI"
label var var9 "Model"

local xlines ""
local xlabels ""
sum l_focus
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum indexc2 if l_focus==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}

	twoway line mean indexc2, sort lwidth(medthin) || line ub indexc2, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb indexc2, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var9 indexc2, sort msize(vsmall)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) ///	
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks Average Passing Rate", size(medium)) subtitle("Conditional on the Previous Difficulty Level Task Performance", size(small)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("The dashed yellow lines indicate the last task at each difficulty level." "Within difficulty levels, tasks are arranged by the order of the children taking them.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin))  ///
	saving("plots/cog/cog_pre1", replace)
	graph export "plots/cog/cog_pre1.pdf", replace	
*/	

	
*******************************************c2 conditional on previous task***************************************

use artc2_rate_pretask, clear
**Paste over lines 261-342 simulated moments - var9
gen index = _n
replace var9 = . if var9 == -99

label var mean "Data"
label var ub "95% CI"
label var var9 "Model"

local xlines ""
local xlabels ""
sum l_focus
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum index if l_focus==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}


twoway line mean index, sort lwidth(medthin) || line ub index, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb index, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var9 index, sort msize(small)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) ///	
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks Average Passing Rate", size(medium)) subtitle(Conditional on Previous Tasks' Performance at the Same Level, size(small)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. The figure presents the average passing rate for each task conditional on passing previous tasks at each level. For example, it shows tasks" "2-5's passing rates conditional on passing the first task, and tasks 3-5's passing rates conditional on passing task 2, etc., at each difficulty level." "2. The dashed yellow lines indicate the last task at each difficulty level.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("plots/cog/cog_pretask", replace)
	graph export "plots/cog/cog_pretask.pdf", replace		


***individual level ones***
/*
	bys l_focus (task_condition): gen index = _n

foreach i in 8 10 12{
	sum index if l_focus == `i'
	local min = `r(min)'
	local max = `r(max)'

twoway connected mean index if l_focus == `i', sort lwidth(medthin) msize(small) || line ub index if l_focus == `i', sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb index if l_focus == `i', sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var9 index if l_focus == `i', sort msize(small)   ///
	xlabel(`min'(1)`max', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Task Order, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks Average Passing Rate (Level `i')", size(medium)) subtitle(Conditional on Previous Tasks' Performance at the Same Level, size(small)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. The figure presents the average passing rate for each task conditional on passing previous tasks at each level. For example, it shows tasks" "2-5's passing rates conditional on passing the first task, and tasks 3-5's passing rates conditional on passing task 2, etc., at difficulty level `i'.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("plots/cog/cog_pretask_l`i'", replace)
	graph export "plots/cog/cog_pretask_l`i'.pdf", replace		



}
*/
	
*******************************************c2 conditional on previous 2 level***************************************

use artc2_rate_pre2, clear
**paste over 343-484 - var9


gen temp = _n

bys l_focus (temp): gen indexc2 = _n

label var mean "Data"
label var ub "95% CI"
label var var9 "Model"
forval i = 3/12{
	if (`i' >= 7 & `i' <= 8) {
		local xlabels `" 3 "1st Task" 8 "2nd Task" 13 "3rd Task" 18 "4th Task" 23 "5th Task" "'
		local xlines "5(5)20"
	}
	else if `i' == 3 | `i' == 4 | `i'==9 {
		local xlabels `" 1 "1st Task" 3 "2nd Task" 5 "3rd Task" 7 "4th Task" 9 "5th Task" "'
		local xlines "2(2)8"
	}
	else if `i' == 5 | `i' == 6 | `i' == 11 {
		local xlabels `" 3 "1st Task" 8 "2nd Task" "'
		local xlines "5"
	}
	else if `i' == 10 {
		local xlabels `" 2.5 "1st Task" 6.5 "2nd Task" 10.5 "3rd Task" 14.5 "4th Task" 20.5 "5th Task" "'
		local xlines "4(4)16"
	}
	else if `i' == 12 {
		local xlabels `" 2 "1st Task" 5 "2nd Task" 8 "3rd Task" 11 "4th Task" "'
		local xlines "3(3)9"
	}	
	twoway connected mean indexc2 if l_focus == `i', sort lwidth(medthin) msize(small) || line ub indexc2 if l_focus == `i', sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb indexc2 if l_focus == `i', sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var9 indexc2 if l_focus == `i', sort msize(small)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Task Order from The Level 2 Steps Down That Is Being Conditional on, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks Average Passing Rate (Level `i')", size(medium)) subtitle("Conditional on Difficulty Level `=`i'-2' Task Performance", size(small)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("The dashed yellow lines indicate the last task at each task from previous level being conditioned on." "Within difficulty levels, tasks are arranged by the order of the children taking them.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin))  ///
	saving("plots/cog/cog_pre2_l`i'", replace)
	graph export "plots/cog/cog_pre2_l`i'.pdf", replace	

}		


/*
gen index = _n
replace var9 = . if var9 == -99


label var mean "Data"
label var ub "95% CI"
label var var9 "Model"


local xlines ""
local xlabels ""
sum l_focus
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum index if l_focus==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}

	twoway line mean index, sort lwidth(medthin) || line ub index, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb index, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var9 index, sort msize(small)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks Average Passing Rate", size(medium)) subtitle("Conditional on the Difficulty Level Two Steps Down Task Performance", size(small)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. The dashed yellow lines indicate the last task of each difficulty level.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin))  ///
	saving("plots/cog/cog_pre2", replace)
	graph export "plots/cog/cog_pre2.pdf", replace	
*/


*******************************************New and old group***************************************
	

use enroll_1mo, clear

keep if levelc2 != .
sort ts_id
**Paste over lines 485-554 simulated moments for new enroll - var40
**Paste over lines 555-624 simulated moments for old enroll - var41
rename var40 sim_new
rename var41 sim_old

replace sim_new = . if sim_new == -99
replace sim_old = . if sim_old == -99

keep if N_old>10 & N_new>10

unab skills : level*
local skills : subinstr local skills "level" "", all
foreach skill of local skills {
	sort level`skill' ts_id
	gen index`skill'= sum(!missing(level`skill')) if !missing(level`skill')
}
unab skills : level*
local skills : subinstr local skills "level" "*", all
order `skills', after(max_age)
sort ts_id



label var mean_new "Data"
label var ub_new "95% CI"
label var sim_new "Model"
label var mean_old "Data"
label var ub_old "95% CI"
label var sim_old "Model"

preserve
drop if mean_new == .
local xlines ""
local xlabels ""
sum levelc2
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum indexc2 if levelc2==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}
***delete less than 10 obs
twoway line mean_new indexc2, sort lwidth(medthin) || line ub_new indexc2, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb_new indexc2, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter sim_new indexc2, sort msize(small)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks for Newly Enrolled Group", size(medium)) subtitle(Enrollment Duration Less than One Month, size(small)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. Enrolled < 1 Month represents children who had been in the program for less than one month when the task was evaluated." "2. Tasks with fewer than 10 observations in either group are omitted.   3. The dashed yellow lines indicate the last task at each difficulty level." "Within difficulty levels, tasks are arranged by the order of the children taking them.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin))  ///
	saving("plots/cog/cog_groupnew", replace)
	graph export "plots/cog/cog_groupnew.pdf", replace	
restore


preserve
drop if mean_old == .
local xlines ""
local xlabels ""
sum levelc2
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum indexc2 if levelc2==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}
	
twoway line mean_old indexc2, sort lwidth(medthin) || line ub_old indexc2, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb_old indexc2, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter sim_old indexc2, sort msize(small)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks for Already Enrolled Group", size(medium)) subtitle(Enrollment Duration More than one month, size(small)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. Enrolled > 1 Month represents children who had been in the program for more than one month when the task was evaluated, who continued" "to stay in the program for 2 years. 2. Tasks with fewer than 10 observations in either group are omitted.  3. The dashed yellow lines indicate" "the last task at each difficulty level. Within difficulty levels, tasks are arranged by the order of the children taking them.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin))  ///
	saving("plots/cog/cog_groupold", replace)
	graph export "plots/cog/cog_groupold.pdf", replace	

restore

	
	
	
	
*******************************************c2 first 5 tasks each level***************************************
use artc21-5_rate, clear

**paste 625-684 - var8
label var var8 "Model"
drop if var8 == -99
gen index = _n

label var mean "Data"
label var ub "95% CI"


local xlines ""
local xlabels ""
sum level
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum index if level==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}

	twoway line mean index, sort lwidth(medthin) || line ub index, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb index, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var8 index, sort msize(small)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Tasks by Level", size(medium)) subtitle(First Few Tasks at Each Level, size(small)) legend(order(1 2 4) row(1) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("Note: The dashed yellow lines indicate the last task at each difficulty level. Within difficulty levels, " "tasks are arranged by the order of the children taking them.", size(small) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin))  ///
	saving("plots/cog/cog_first5_alllevel", replace)
	graph export "plots/cog/cog_first5_alllevel.pdf", replace	
		
		

	
********Duration******
use duration_c2, clear
**paste over lines 685-697 var8

label var mean "Data"
label var ub "95% CI"
label var var8 "Model"

twoway connected mean level, sort lwidth(medthin) msize(small) || line ub level, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb level, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var8 level, sort msize(small)   ///
	xlabel(1(1)13, labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Time to Mastery, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Time to Mastery by Level", size(medium)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("Note: Time to Mastery is defined as the number of tasks a child takes at the previous difficulty level" "until the first success (inclusive).", size(small) span)   ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("plots/cog/cog_dur", replace)
	graph export "plots/cog/cog_dur.pdf", replace	
	
	

********Interaction******
use interaction_c2, clear


**paste over lines 711-749 var8

drop if level == 13
reshape wide mean n se lb ub var8, i(level) j(reshape)

foreach i in mean n se lb ub var8{
    rename `i'1 `i'_tab
    rename `i'2 `i'_intab
    rename `i'3 `i'_inttc	
}

foreach i in _tab _intab _inttc{
	label var mean`i' "Data"
	label var ub`i' "95% CI"
	label var var8`i' "Model"
}


twoway connected mean_tab level, sort lwidth(medthin) msize(small) || line ub_tab level, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb_tab level, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var8_tab level, sort msize(small)   ///
	xlabel(1(1)12, labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Correlation, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Correlation between Duration and Teaching Ability", size(medium)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("plots/cog/cog_tab", replace)
	graph export "plots/cog/cog_tab.pdf", replace	
	

twoway connected mean_intab level, sort lwidth(medthin) msize(small) || line ub_intab level, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb_intab level, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var8_intab level, sort msize(small)   ///
	xlabel(1(1)12, labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Correlation, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Correlation between Duration" "and Interaction Quality between Caregiver and Home Visitor", size(medium)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("plots/cog/cog_intab", replace)
	graph export "plots/cog/cog_intab.pdf", replace	
	
twoway connected mean_inttc level, sort lwidth(medthin) msize(small) || line ub_inttc level, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || line lb_inttc level, sort lcolor(gs11) lpattern(dash) lwidth(medthin) || scatter var8_inttc level, sort msize(small)   ///
	xlabel(1(1)12, labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Correlation, size(small)) ylabel(,labsize(small)) ///
	title("Fit for Cognitive Correlation between Duration" "and Interaction Quality between Home Visitor and Child", size(medium)) legend(order(1 2 4) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("plots/cog/cog_inttc", replace)
	graph export "plots/cog/cog_inttc.pdf", replace	


		